import MComponent from '../../common/MComponent'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import { store } from '../../store/index'
import { _consultcode } from '../../api/member'
import { _bindJuke } from '../../api/consult'
const app = getApp()
MComponent({
  behaviors: [storeBindingsBehavior],
  storeBindings: {
    store,
    fields: {
      member: store => store.member
    }
  },
  data: {
    qrcode: '',
    codeShow: false,
    brightness: null,
    conflict: true,
    formShow: false,
    jukeCode: ''
  },
  methods: {
    showCode() {
      const member = wx.getStorageSync('member')
      const ConsultantID = member ? member.ConsultantID : ''
      if (!ConsultantID) {
        app.toast('您不是置业顾问')
        return
      }
      const { qrcode } = this.data
      if (qrcode) {
        wx.getScreenBrightness({
          success: res => {
            this.data.brightness = res.value
            wx.setScreenBrightness({
              value: 1
            })
          }
        })
        this.set({
          codeShow: true
        })
      } else {
        app.loading('加载中')
        _consultcode({ ConsultantID })
          .then(res => {
            wx.hideLoading()
            const { code, msg, data } = res.data
            if (code === 0) {
              wx.getScreenBrightness({
                success: res => {
                  this.data.brightness = res.value
                  wx.setScreenBrightness({
                    value: 1
                  })
                }
              })
              this.set({
                qrcode: data,
                codeShow: true
              })
            } else {
              wx.showModal({
                title: '对不起',
                content: msg,
                showCancel: false
              })
            }
          })
          .catch(err => {
            wx.hideLoading()
            console.log(err)
          })
      }
    },
    hideCode() {
      const { brightness } = this.data
      console.log(brightness)
      wx.setScreenBrightness({
        value: brightness
      })
      this.set({
        codeShow: false
      })
    },
    // 保存
    saveImg() {
      wx.showActionSheet({
        itemList: ['保存图片'],
        success: res => {
          if (res.tapIndex === 0) {
            const {
              qrcode
            } = this.data
            if (!qrcode) {
              return
            }
            app.loading('保存中')
            wx.downloadFile({
              url: qrcode,
              success: res => {
                wx.saveImageToPhotosAlbum({
                  filePath: res.tempFilePath,
                  success: result => {
                    wx.hideLoading()
                    wx.showToast({
                      title: '保存成功',
                      icon: 'success',
                    })
                  },
                  fail: () => {
                    wx.hideLoading()
                    wx.showToast({
                      title: '图片下载失败',
                      icon: 'none'
                    })
                  }
                })
              },
              fail: () => {
                wx.hideLoading()
                wx.showToast({
                  title: '图片下载失败',
                  icon: 'none'
                })
              }
            })
          }
        },
        fail: res => {
          console.log(res.errMsg)
        }
      })
    },
    onShow() {
      // app.loading('加载中')
      wx.showNavigationBarLoading()
      app.checkAuth()
        .then(res => {
          const uid = res
          return app.getUserInfoByUid(uid)
        })
        .then(memberInfo => {
          // wx.hideLoading()
          wx.hideNavigationBarLoading()
          this.set({
            member: memberInfo
          })
        })
        .catch(err => {
          // wx.hideLoading()
          wx.hideNavigationBarLoading()
          console.log(err)
          wx.removeStorageSync('member')
          store.updateMember(null)
        })
    },
    showForm () {
      this.set({
        formShow: true
      })
    },
    hideForm () {
      this.set({
        formShow: false
      })
    },
    onInput (e) {
      const { value: jukeCode } = e.detail
      this.set({
        jukeCode
      })
    },
    bindJuke () {
      const member = wx.getStorageSync('member')
      const ConsultantID = member ? member.ConsultantID : ''
      if (!ConsultantID) {
        app.toast('您不是置业顾问')
        return
      }
      const { jukeCode: JuKeCode } = this.data
      if (!JuKeCode) {
        app.toast('矩客码不能为空')
        return
      }
      const UnionID = member.UnionID
      app.loading('提交中')
      _bindJuke({ UnionID, JuKeCode })
        .then(res => {
          wx.hideLoading()
          const { code, msg } = res.data
          wx.showModal({
            title: code === 0 ? '温馨提示' : '对不起',
            content: msg,
            showCancel: false,
            success: r => {
              if (r.confirm && code === 0) {
                this.hideForm()
              }
            }
          })
        })
        .catch(err => {
          wx.hideLoading()
          console.log(err)
          wx.showModal({
            title: '对不起',
            content: JSON.stringify(err),
            showCancel: false
          })
        })
    },
    onHide() {
      this.hideCode()
    },
    onUnload() {
      this.hideCode()
    }
  }
}, true)